package api

import (
	"Holidays/model"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// CheckHoliday 检查今天是否是节假日
func CheckHoliday(c *gin.Context) {
	year := time.Now().Year()
	today := time.Now().UTC().Truncate(24 * time.Hour) // 去除时间部分，只保留日期
	holidays, ok := model.HolidaysMap[year]
	if !ok || len(holidays) == 0 {
		c.JSON(http.StatusNotFound, gin.H{"error": "根据当前时间查询失败啦"})
		return
	}
	isHoliday := false
	nextHolidayName := ""
	daysToNextHoliday := 0
	for _, holiday := range holidays {
		if holiday.Date.Equal(today) {
			isHoliday = true
			nextHolidayName = holiday.Name
			daysToNextHoliday = 0
			break
		}
		if holiday.Date.After(today) {
			nextHolidayName = holiday.Name
			daysToNextHoliday = int(holiday.Date.Sub(today).Hours() / 24)
			break
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"今天是不是节假日":    isHoliday,
		"下一个节假日":      nextHolidayName,
		"距离下一个节假日的天数": daysToNextHoliday,
	})
}
